<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
    <style>
        .box {
            width: 80%;
            margin: 10px auto;
            min-height: 320px;
            border: 1px darkgoldenrod solid;
            border-radius: 10px;
        }

        .router-link-exact-active,
        .router-link-active {
            background-color: darkgoldenrod;
            color: black;
        }
    </style>
</head>

<body>
    <div id="app">
        <h1>{{title}}</h1>
        <hr>
        <!-- 5.路由出口 在哪里显示 路由组件 -->
        <div class="box">
            <router-view></router-view>
        </div>

        <!-- 6.路由导航 声明式导航 -->
        <div>
            <router-link to="/home">首页</router-link>
            <router-link to="/find">发现</router-link>
            <router-link to="/mine">个人中心</router-link>
            <router-link to="/list">列表</router-link>
            <router-link to="/not-found">未定义</router-link>

        </div>
    </div>


    <template id="find">
        <div>
            <h2>find---find---find</h2>

        </div>
    </template>

    <template id="mine">
        <div>
            <h2>mine---mine---mine</h2>
        </div>
    </template>

    <template id="list">
        <div>
            <h2>List---list---list</h2>
        </div>
    </template>

    <template id="notFound">
        <div>
            <h2>没有找到页面嘞</h2>
        </div>
    </template>

    <script>
        // 0.如何使用模块化机制编程 ,导入Vue和VueRouter,要调用Vue.use(VueRouter)

        // 1.定义(路由)组件
        const Home = {
            template: "<div>Home---Home---Home</div>"
        }
        const Find = {
            template: "#find"
        }
        const Mine = {
            template: "#mine"
        }
        const List = {
            template: "#list"
        }
        const NotFound = {
            template: "#notFound"
        }

        // 2.定义路由 定义好路由的匹配规则
        // 每个路由都应该隐射一个组件

        const routes = [
            { path: '/', component: NotFound },/* 如果是不存在的组件 默认返回404 */
            { path: '/home', component: Home },
            { path: '/find', component: Find },
            { path: '/mine', component: Mine },
            { path: '/list', component: List },
            { path: '/notFound', component: NotFound },
            { path: '*', component: NotFound },

        ]

        // 3.创建路由实例对象
        const router = new VueRouter({
            routes,
        })

        // 4.挂载到根实例
        const vm = new Vue({
            router,
            watch: {
                '$route': function (to, from) {
                    console.log(to);
                    console.log(from);
                }
            },
            data: {
                title: "Vue Router 入门学习",
            },
            mounted() {
                console.log(this.$router);
                console.log(this.$route);

            }
        }).$mount("#app")
    </script>
</body>

</html>